package com.ls.service.user;

import com.ls.dao.BaseDao;
import com.ls.dao.user.UserDao;
import com.ls.dao.user.UserDaoImpl;
import com.ls.pojo.User;
import com.mysql.jdbc.Driver;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 李朔
 * @version 1.0
 */
//业务层都需要调用DAO层，所以我们要引入DAO层
public class UserServiceImpl implements UserService {
    private UserDao userDao;
    //构造器
    public UserServiceImpl() {
        userDao=new UserDaoImpl();
    }

    public User login(String userCode, String password) throws ClassNotFoundException, SQLException {
        Connection connection=null;
        User user=null;
        //通过业务层调用对应的具体的数据库操作
        connection= BaseDao.getConnection();
        user = userDao.getLoginUser(connection, userCode);
        BaseDao.closeResource(connection,null,null);
        return user;
    }

    public boolean updatePwd(int id, String password) throws SQLException {
        Connection connection=null;
        boolean flag=false;
        try{
            connection= BaseDao.getConnection();
            //修改密码
            if(userDao.updatePwd(connection,id,password)>0){
                //SQL执行成功了
                flag=true;
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    public int getUserCount(String username, int userRole) throws ClassNotFoundException, SQLException {
        Connection connection = BaseDao.getConnection();
        int userCount = userDao.getUserCount(connection, username, userRole);
        BaseDao.closeResource(connection,null,null);
        return userCount;
    }
    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) throws SQLException {
        // TODO Auto-generated method stub
        Connection connection = null;
        List<User> userList = null;
//        System.out.println("queryUserName ---- > " + queryUserName);
//        System.out.println("queryUserRole ---- > " + queryUserRole);
//        System.out.println("currentPageNo ---- > " + currentPageNo);
//        System.out.println("pageSize ---- > " + pageSize);
        try {
            connection = BaseDao.getConnection();
            userList = userDao.getUserList(connection, queryUserName,queryUserRole,currentPageNo,pageSize);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return userList;
    }
    //添加用户
    public boolean addUser(User user) throws SQLException {
        Connection connection=null;
        boolean flag=false;
        try {
            //建立连接
            connection=BaseDao.getConnection();
            connection.setAutoCommit(false);//把自动提交事务取消掉
            flag= userDao.addUser(connection, user);
            connection.commit();
            if(flag){
                System.out.println("添加成功");
            }else{
                System.out.println("添加失败");
            }

        } catch (Exception e) {
            //发送错误，就回滚事务
            connection.rollback();
        }
        finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    public User selectUserCodeExist(String userCode) throws SQLException {
        // TODO Auto-generated method stub
        Connection connection = null;
        User user = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getLoginUser(connection, userCode);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }

    @Test
    public void test() throws SQLException, ClassNotFoundException {
        UserServiceImpl userService = new UserServiceImpl();
        int userCount = userService.getUserCount(null, 0);
        System.out.println("count:"+userCount);
    }
}
